﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Linq.Expressions;
using System.Security.Cryptography.X509Certificates;
using System.Threading.Tasks;
using AutoMapper;
using CCcteg.Common;
using Ccteg.Common;
using Ccteg.DB;
using LinqKit;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;

namespace Cctegaq.Api.Controllers.Api
{

    /// <summary>
    /// 安全文化
    /// </summary>
    [ApiController]
    [Route("api/[controller]/[action]")]
    public class SCLController : ControllerBase
    {
        private readonly IMapper _mapper;
        private readonly MySqlDbContext _dbContext;


        public SCLController(MySqlDbContext dbContext, IMapper mapper)
        {

            _dbContext = dbContext;
            _mapper = mapper;
        }
        /// <summary>
        /// 安全文化 添加 修改
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public ResponseModel SCLAddOrUpdate([FromBody] Scl_Scl_ViewModel model)
        {
            var response = ResponseModelFactory.CreateInstance;
            try
            {
                using (_dbContext)
                {
                    var entity = _mapper.Map<Scl_Scl_ViewModel, Scl_Scl>(model);
                    var entryEntity = _dbContext.SCL_Scl.Attach(entity);
                    if (entity.Id > 0)
                    {
                        var editentity = _dbContext.SCL_Scl.AsNoTracking().Where(x => x.Id == entity.Id && x.Deleted == false).AsNoTracking().FirstOrDefault();
                        if (editentity == null)
                        {
                            response.SetFailed("无法修改，数据不存在请刷新页面");
                            return response;
                        }

                        entryEntity.State = EntityState.Modified;
                        entryEntity.Property(p => p.CreatedOn).IsModified = false;
                        entryEntity.Property(p => p.CreatedBy).IsModified = false;
                        entryEntity.Property(p => p.CompanyName).IsModified = false;
                        entryEntity.Property(p => p.CompanyNo).IsModified = false;
                        entryEntity.Property(p => p.Deleted).IsModified = false;
                        entryEntity.Property(p => p.State).IsModified = false;
                        entity.ModifiedBy = "系统";
                        entity.ModifiedOn = System.DateTime.Now;
                    }
                    else
                    {
                        entryEntity.Property(p => p.ModifiedBy).IsModified = false;
                        entryEntity.Property(p => p.ModifiedOn).IsModified = false;
                        entity.Deleted = false;
                        entity.CreatedBy = "系统";
                        entity.CreatedOn = System.DateTime.Now;
                        entity.CompanyName = " 中国煤科";
                        entity.CompanyNo = "001";
                        entity.State = "未操作";

                    }
                    _dbContext.SaveChanges();
                    response.SetSuccess("操作成功");

                }
            }
            catch (Exception ex)
            {
                response.SetFailed("操作失败");
                response.Data = ex.Message;
            }
            return response;
        }


        /// <summary>
        /// 安全文化 删除
        /// </summary>
        /// <param name="id">id</param>
        /// <returns></returns>
        [HttpPost]
        public ResponseModel SCLDelete([Required] int id)
        {
            var response = ResponseModelFactory.CreateInstance;
            try
            {
                using (_dbContext)
                {
                    var res = _dbContext.SCL_Scl.Where(x => x.Id == id)
                        .SetField(x => x.Deleted).WithValue(true)
                        .SetField(x => x.ModifiedOn).WithValue(DateTime.Now)
                        .SetField(x => x.ModifiedBy).WithValue("系统")
                        .Update();
                    if (res > 0)
                        response.SetSuccess("删除成功");
                    else
                        response.SetFailed("删除失败");

                }

            }
            catch (Exception ex)
            {
                response.SetFailed("删除失败");
                response.Data = ex.Message;
            }
            return response;
        }

        /// <summary>
        /// 安全文化 详情
        /// </summary>
        /// <param name="id">id</param>
        /// <returns></returns>
        [HttpGet]
        public ResponseModel<Scl_Scl_ViewModel> SCLDetail([Required] int id)
        {
            using (_dbContext)
            {
                var response = new ResponseModel<Scl_Scl_ViewModel>();
                var query = _dbContext.SCL_Scl.AsNoTracking().Where(x => x.Id == id && x.Deleted == false).FirstOrDefault();
                var data = _mapper.Map<Scl_Scl, Scl_Scl_ViewModel>(query);
                response.SetData(data);
                return response;
            }

        }

        /// <summary>
        /// 安全文化查询
        /// </summary>
        /// <param name="companyno">单位编码</param>
        /// <param name="culactname">活动名称</param>
        /// <param name="methods">会议类型(名称)</param>
        /// <param name="year">年度</param>
        /// <param name="state">状态(未操作, 已分享, 已拒绝)</param>
        /// <param name="pageindex">页码</param>
        /// <param name="pagesize">显示条数</param>
        /// <returns>已上参数为空时为全部</returns>
        [HttpGet]
        public ResponseResultModel<IEnumerable<Scl_Scl_ViewModel>> SclSclSearch(string companyno, string state, string culactname, string methods, string year, int pageindex = 1, int pagesize = 10)
        {
            using (_dbContext)
            {


                var response = ResponseModelFactory<IEnumerable<Scl_Scl_ViewModel>>.CreateResultInstance;
                Expression<Func<Scl_Scl, bool>> where = p => true;

                where = where.CheckNullAnd(x => x.CompanyNo == companyno, companyno);
                where = where.CheckNullAnd(x => x.State == state, state);
                where = where.CheckNullAnd(x => x.CulActName.Contains(culactname), culactname);
                where = where.CheckNullAnd(x => x.Methods == methods, methods);

                where = where.CheckNullAnd(x => x.Year == year, year);
                where = where.And(x => x.Deleted == false);
                var query = _dbContext.SCL_Scl.AsQueryable();

                query = query.Where(where.Expand()).OrderByDescending(x => x.Id);

                var list = query.Paged(pageindex, pagesize).ToList();
                var totalCount = query.Count();
                var data = list.Select(_mapper.Map<Scl_Scl, Scl_Scl_ViewModel>);
                response.SetData(data, totalCount);
                return response;
            }

        }
    }

}
